using LuaInterface;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class ReturnDrag : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        // SendRequest("http://192.168.101.50:8080/res/res.json", (isok, strdata) =>
        // {
        //     if (isok)
        //     {
        //         Debug.Log("Response: " + strdata);
        //     }
        //     else
        //     {
        //         Debug.LogError("Request failed");
        //     }
        // });
    }

    public void SendRequest(string url, Action<bool, string> callback, WWWForm form = null)
    {
        StartCoroutine(SendRequestCoroutine(url, callback, form));
    }

    private IEnumerator SendRequestCoroutine(string url, Action<bool, string> callback, WWWForm form = null)
    {
        using (UnityWebRequest www = form != null ? 
            UnityWebRequest.Post(url, form) : 
            UnityWebRequest.Get(url))
        {
            yield return www.SendWebRequest();

            if (www.result == UnityWebRequest.Result.Success)
            {
                string data = www.downloadHandler.text;
                callback?.Invoke(true, data);
            }
            else
            {
                Debug.LogError("Error: " + www.error);
                callback?.Invoke(false, null);
            }
        }
    }

    public float minDragDis = 100;
    public float minDifDis = 100;
    public float minSpeed = 1.5f;

    float downX;
    float upX;
    double downTime;
    double upTime;

    bool isReturn = false;

    void iosReturn()
    {
        if (Input.touchCount == 1)
        {

            if (Input.touches[0].phase == TouchPhase.Began)
            {


                downX = Input.mousePosition.x;
                downTime = DateTime.Now.TimeOfDay.TotalMilliseconds;
               // Debug.LogError("x" + downX);
                if ((downX < minDifDis) || (downX > Screen.width - minDifDis))
                {
                    isReturn = true;
                }

            }
            if (Input.touches[0].phase == TouchPhase.Ended)
            {
                if (!isReturn)
                {
                    return;
                }

                upTime = DateTime.Now.TimeOfDay.TotalMilliseconds;
                upX = Input.mousePosition.x;

                double speed = (upX - downX) / (upTime - downTime);
                float dis = (upX - downX);
                if (Math.Abs(dis) > minDragDis && Math.Abs(speed) > minSpeed)
                {
                    if (LuaMgr.GetMainState() != null)
                    {
                        LuaFunction func = LuaMgr.GetMainState().GetFunction("QuitTool.QuitTool.IosReturn");
                        if (func != null)
                        {
                            func.BeginPCall();
                            func.PCall();
                            func.EndPCall();
                            func.Dispose();
                        }
                    }
                }
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
#if UNITY_IPHONE
        iosReturn();
#endif
    }
}
